let polynome = [(1,1);(5,2);(2,3);(7,6)];;


let rec power x n = if n = 0 then 1 
  else x * power x (n-1);;


let rec application poly x =
  match poly with 
      []->0
    | (y,z)::l -> (y * power x z) + application l x;;

application [(2,2);(2,3);(4,4)] 2;;


let rec add x y = 
  match (x,y) with
      ([], []) -> []
    | ([], ((a,b)::l)) | (((a,b)::l), []) -> (a,b)::l
    | (((a,b)::l1),((c,d)::l2)) when b = d -> if a = (-c) then add l1 l2 else
	  ((a+c), b):: add l1 l2
    | (((a,b)::l1),((c,d)::l2)) -> if b < d then 
	(a,b)::add l1 ((c,d)::l2) else
	(c,d)::add ((a,b)::l1) l2;;

add [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;


let rec soustract x y =
  match (x,y) with
      ([], []) -> []
    | ([], ((a,b)::l))-> (-a,b)::soustract [] l
    | (((a,b)::l), []) -> (a,b)::l
    | (((a,b)::l1),((c,d)::l2)) when b = d -> if a = c then soustract l1 l2 else
	  ((a-c), b):: soustract l1 l2
    | (((a,b)::l1),((c,d)::l2)) -> if b < d then 
	(a,b)::soustract l1 ((c,d)::l2) else
	((-c),d)::soustract ((a,b)::l1) l2;;

soustract [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;


let rec deriv x =
  match x with
      []->[]
    | (a,b)::l -> ((a*b),(b-1))::deriv l;;

deriv [(2,2);(2,3);(4,4)];;
